home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-29 | 5.3 KB | 164 lines | [TEXT/KAHL] |
- // ** TRegion.h **
- //
- // TRegion - class wrapper for Quickdraw regions
- //
- // Chris Prinos ©1993, Symantec Corp
- //
- // TRegion is a class wrapper for the Quickdraw Region
- //type that provides overloaded operators to simplify //expressions with regions and other Quickdraw entities used
- // to form regions.
- //
- // TRegions can be used in mixed expressions such as
- //x = a <op> b, where:
- //
- // • x is a TRegion
- //
- // • a and b are one of: - TRegion
- // - RgnHandle
- // - PicHandle
- // - Rect
- // - PolyHandle
- //
- // • <op> is one of: + union of a and b
- // - difference between a and b (b from a)
- // ^ XOR of a and b
- // & intersection of a and b
- // | union of a and b (same as + )
- //
- // • operators can be chained, like x = a ^ b | (c-(d & f));
- //
- // • operators +=, -=, ^=, |=, &= are also provided and
- // are more efficient than their expanded versions,
- // i.e., use a += c instead of a = a + c for
- // best performance
- //
- // • Boolean operators ||, &&, ==, and ! are provided.
- // If a TRegion is evaluated in the context of a boolean
- // expression, it is true if the region is nonempty.
- // a == b is true if and b describe the same region
- //
- // TRegions can be constructed in a variety of ways. In all
- // cases, the constructor takes care of allocating space for
- // the private RegionData. The destructor disposes of any
- // allocated memory for a region. The constructors
- // can be specified by any of the following:
- //
- // • default - creates a nil region
- // • a RgnHandle - makes a TRegion with the
- // RgnHandle (doesn't copy)
- // • a PicHandle - makes a TRegion that surrounds a
- // Macintosh picture
- // • a Rect - makes a TRegion that surround the Rect
- // • a PolyHandle - makes a TRegion that surrounds
- // the Polygon
- // • TRegion - makes a TRegion with another TRegion
- // (copy ctor)
- //
- // Conversion operators: TRegion defines several conversion
- // operators. Constructors provide conversions to the type
- // TRegion from the following: RgnHandle, PicHandle, Rect,
- // PolyHandle. In addition, a conversion operator is provided
- // to convert from TRegion to RgnHandle so that TRegions can
- // be used anywhere a RgnHandle is called for, and from
- // TRegion to int so that TRegions can appear in boolean
- // expressions
- //
- // Utility functions. The following utility functions
- //are provided for TRegion:
- //
- // • Empty() - will empty the TRegion
- // • IsEmpty() - returns true if the specified
- // TRegion is true
- // • Offset() - same as OffsetRgn(), but it can take
- // a Point parameter
- // • Inset() - same as InsetRgn(), but it can take
- // a Point parameter
- // • ContainsRect - same as RectInRgn()
- // • ContainsPoint - same as PtInRgn()
- //
- // TRegion uses a nested RegionData class to facilitate
- //reference counting. The reference counting eliminates copy
- //and assignment overhead, as well as reducing storage
- //requirements
- #pragma once
-
- class TRegion
- {
- public:
- // constructors (and implied conversion operators)
- TRegion();
- TRegion(const RgnHandle r); // doesn't copy data
- TRegion(const Rect &r);
- TRegion(const TRegion &r);
- TRegion(short left, short top, short right, short bottom);
- TRegion(const PolyHandle p);
- TRegion(const PicHandle p);
-
- // destructors
- ~TRegion();
-
- // Utility member functions
- void Empty();
- Boolean IsEmpty() const;
- void Offset(short h, short v);
- void Offset(Point p);
- void Inset(short h, short v);
- void Inset(Point p);
- Boolean ContainsRect(const Rect &r) const;
- Boolean ContainsPoint(Point p) const;
-
- // member function operator overloads
- TRegion& operator=(const TRegion &r); // assignment
-
- operator RgnHandle() // conversion to RgnHandle
- { return fRgn->fRgnH; }
- operator int() // Boolean conversion
- { return !EmptyRgn(fRgn->fRgnH); } // true if nonempty
-
- TRegion& operator+=(const TRegion &r);
- TRegion& operator-=(const TRegion &r);
- TRegion& operator^=(const TRegion &r);
- TRegion& operator|=(const TRegion &r);
- TRegion& operator&=(const TRegion &r);
- Boolean operator!()
- { return EmptyRgn(fRgn->fRgnH); }
-
- // non-member operator overloads
- // these operators need to be friends since they access
- // private members they are defined as non-member functions
- // for mixed type expressions
- friend TRegion operator+(const TRegion &a,
- const TRegion &b);
- friend TRegion operator-(const TRegion &a,
- const TRegion &b);
- friend TRegion operator&(const TRegion &a,
- const TRegion &b);
- friend TRegion operator|(const TRegion &a,
- const TRegion &b);
- friend TRegion operator^(const TRegion &a,
- const TRegion &b);
- friend Boolean operator==(const TRegion &a,
- const TRegion &b);
- friend Boolean operator&&(const TRegion &a,
- const TRegion &b);
- friend Boolean operator||(const TRegion &a,
- const TRegion &b);
-
- private:
- struct RegionData // nested data class
- {
- RgnHandle fRgnH; // Mac Region handle
- short fCount; // reference count
-
- RegionData()
- { fCount = 1; fRgnH = NewRgn(); }
- RegionData(RgnHandle r)
- { fCount = 1; fRgnH = r; }
- ~RegionData()
- { DisposeRgn (fRgnH); }
- };
- RegionData *fRgn;
-
- void DetachSharedRgn(); // disconnect from shared RegionData object
- };
-